<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#list entities.entity as entity>
    <@pp.changeOutputFile name="${javaDir}${packageDir}/dao/"+entity.@name?cap_first+"DaoBase.java" />
    <#assign entityName = entity.@name>
package ${project.@rootPackage}.dao;

import ${project.@rootPackage}.dao.support.AbstractDao;
import ${project.@rootPackage}.domain.${entityName};
    <#list entity["one-to-one"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>
    <#list entity["many-to-one"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>
    <#list entity["many-to-many"] as rel>
import ${project.@rootPackage}.domain.${rel.@entity};
    </#list>

<#include "/includes/fixImportsList.inc">
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Dao implementation for entity ${entityName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class ${entityName}DaoBase extends AbstractDao<${entityName}> {

	protected static final Logger logger = Logger.getLogger("${entityName}Dao");

    @PersistenceContext
    protected EntityManager em;

    public ${entityName}DaoBase() {
        super(${entityName}.class);
    }

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public EntityManager getEntityManager() {
        return em;
    }
    
    <#if entity.column.@name?seq_contains("baja")>
    public List<${entityName}> findAllNotBaja() {
        Query q = em.createQuery("select ${entityName?uncap_first} from ${entityName} ${entityName?uncap_first} where ${entityName?uncap_first}.baja=false");
        return q.getResultList();
    }
    </#if>
    
    <#list entity["one-to-one"] as rel>
    public ${entityName} findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}) {
        Query q = em.createQuery("select ${entityName?uncap_first} from ${entityName} ${entityName?uncap_first} where ${entityName?uncap_first}.${rel.@name?uncap_first}.id=:id${rel.@entity}");
        q.setParameter("id${rel.@entity}",${rel.@name?uncap_first}.getId());
        ${entityName} obj = null;
        try {
            obj = (${entityName})q.getSingleResult();
        } catch (NoResultException e) {
        }
        return obj;
    }
	
    </#list>

    <#list entity["many-to-one"] as rel>
    public List<${entityName}> findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}) {
        Query q = em.createQuery("select ${entityName?uncap_first} from ${entityName} ${entityName?uncap_first} where ${entityName?uncap_first}.${rel.@name?uncap_first}.id=:id${rel.@entity}");
        q.setParameter("id${rel.@entity}", ${rel.@name?uncap_first}.getId());
        return q.getResultList();
    }
	
    </#list>
    <#list entity["many-to-many"] as rel>
    public List<${entityName}> findBy${rel.@entity}(${rel.@entity} ${rel.@name?uncap_first}) {
        Query q = em.createQuery("select ${entityName?uncap_first} from ${entityName} ${entityName?uncap_first} where ${entityName?uncap_first}.${rel.@name?uncap_first}.id=:id${rel.@entity}");
        q.setParameter("id${rel.@entity}",${rel.@name?uncap_first}.getId());
        return q.getResultList();
    }
	
    </#list>  


  	<#if includeLoginJsf && (entityName = config.login.@entity[0]) >
    public ${entityName} findByLoginAndPassword(String user,String password){
        ${entityName} result=null;
        Query q = em.createQuery("select u from ${entityName} u where u.login=:user and u.password=:password");
        q.setParameter("user", user);
        q.setParameter("password", password);
        try {
            result = (${entityName}) q.getSingleResult();
        } catch (javax.persistence.NoResultException e) {
            //no existe
        }
        return result;
    }
    </#if>
    <#if includeLoginSpring && (entityName = config.login.@entity[0]) >
     public ${entityName} findByLogin(String user){
        ${entityName} result=null;
        try {
            result = (${entityName})em.createQuery("select u from ${entityName} u where u.login=:user")
                    .setParameter("user", user)
                    .getSingleResult();
        } catch (javax.persistence.NoResultException e) {
            //no existe
        }
        return result;
    }
    </#if>
}
</#list>